﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using SyCODE.Blog.Task;
using SyCODE.Component.DataFactory;
using System.IO;
using System.Xml;
using SyCODE.Component;

namespace SyCODE.Blog.Task
{
    /// <summary>
    /// 文章评论的任务实现
    /// </summary>
    [Serializable]
    public class DeleteAlbumCommentTask : TaskBase
    {
        /// <summary>
        /// 文章评论任务的反序列化构造方法
        /// </summary>
        /// <param name="info">反序列化所需的数据</param>
        /// <param name="context">反序列化源的上下文</param>
        protected DeleteAlbumCommentTask(SerializationInfo info, StreamingContext context)
        {
            Params = (NameValueCollection)info.GetValue("params", typeof(NameValueCollection));
        }

        /// <summary>
        /// 文章评论的任务实现的构造函数
        /// </summary>
        /// <param name="_params">评论任务所需要的键值对数据</param>
        public DeleteAlbumCommentTask(NameValueCollection _params)
        {
            Params = _params;
        }

        /// <summary>
        /// 评论任务的执行方法
        /// </summary>
        public override void Execute()
        {
            string path = Path.Combine(AppPath.GetUserPhysicalPath(Params["u"]), @"album\comment\" + Params["id"] + @"\" + Params["page"] + ".xml");
            XmlDocument commentDom = new XmlDocument();
            commentDom.Load(path);

            XmlNodeList comments = commentDom.DocumentElement.SelectNodes("body/div");
            int index = Int32.Parse(Params["idx"]);

            if (comments.Count > index)
            {
                XmlElement commentNode = (XmlElement)comments[index];

                commentNode.SetAttribute("deleted", "1");
                commentNode.InnerText = "该评论已被博主删除";
                commentDom.Save(path);
            }
        }
    }
}